﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;

namespace CommonLib.Log
{
    public class LogWriter
    {
        public static List<string> ReadLogMsg(int type, string logPath)
        {
            if (logPath.IsNullOrEmpty())
            {
                logPath = string.Format("{0}{1}\\{2}",ConfigurationManager.AppSettings["CommonLog"].ToString(), GetLogSubFolder(), "Common");
            }
            else
            {
                logPath = string.Format("{0}{1}\\{2}", ConfigurationManager.AppSettings["CommonLog"].ToString(), GetLogSubFolder(), logPath);
            }

            return Logger.getInstance().ReadFromLog(type, logPath);
        }

        public static void LogMsg(int type, string info, object isLog)
        {
            if (Convert.ToBoolean(isLog.ToString()) != null)
            {
                string logPath = string.Format("{0}{1}\\{2}", ConfigurationManager.AppSettings["CommonLog"].ToString(), DateTime.Now.ToString("yyyyMMdd"), "Common");
                Logger.getInstance().WriteToLog(type, info, logPath);
            }
        }

        /// <summary>
        /// 写文本日志
        /// </summary>
        /// <param name="fileName">日志文件名称</param>
        /// <param name="content">日志内容</param>
        /// <param name="folderName">在配置的日志路径下单独存放的目录，可为空</param>
        /// <param name="prefixTime">日志内容前是否需要添加时间</param>
        public static void LogMsg(string fileName, string content, string folderName, bool prefixTime)
        {
            Logger.getInstance().WriteToLog(fileName, content, ConfigurationManager.AppSettings["CommonLog"].ToString() + "\\" + folderName + "\\", prefixTime);
        }

        public static void LogMsg(int type, string info, object isLog, string logPath)
        {
            if (Convert.ToBoolean(isLog.ToString()) && ConfigurationManager.AppSettings["CommonLog"] != null)
            {
                if (logPath.IsNullOrEmpty())
                {
                    logPath = string.Format("{0}{1}\\{2}", ConfigurationManager.AppSettings["CommonLog"].ToString(), GetLogSubFolder(), "Common");
                }
                else
                {
                    logPath = string.Format("{0}{1}\\{2}", ConfigurationManager.AppSettings["CommonLog"].ToString(), GetLogSubFolder(), logPath);
                }

                Logger.getInstance().WriteToLog(type, info, logPath);
            }
        }

        /// <summary>
        /// 在保存的日志目录下，复制文本文件到新的文件夹下
        /// </summary>
        /// <param name="sourceFolderName">原目录文件夹名称</param>
        /// <param name="sourceFileName">原文件名称</param>
        /// <param name="targetFolderName">目标目录文件夹名称</param>
        /// <param name="targetFileName">新文件名称</param>
        public static void CopyLogToFile(string sourceFolderName, string sourceFileName, string targetFolderName, string targetFileName)
        {
            Logger.getInstance().CopyLogToFile(ConfigurationManager.AppSettings["CommonLog"].ToString() + "\\" + sourceFolderName + "\\", sourceFileName, ConfigurationManager.AppSettings["CommonLog"].ToString() + "\\" + targetFolderName + "\\", targetFileName);
        }

        /// <summary>
        /// 获取日志文件的子目录
        /// </summary>
        /// <returns></returns>
        public static string GetLogSubFolder()
        {
            string subFolder = DateTime.Now.ToString("yyyyMMdd");

            return subFolder;
        }

        /// <summary>
        /// 文本日志 清理
        /// </summary>
        /// <param name="saveDay">保留多少天</param>
        public static void TxtLogClear(int saveDay)
        {
            Logger.getInstance().TxtLogClear(saveDay, ConfigurationManager.AppSettings["CommonLog"].ToString());
        }

        /// <summary>
        /// 文本日志及空文件夹 清理
        /// </summary>
        /// <param name="DTNow">当前时间</param>
        /// <param name="saveDay">保留多少天</param>
        /// <param name="LogPath">日志保存路径</param>
        public static void TxtLogFolderClear(DateTime DTNow, int saveDay)
        {
            Logger.getInstance().TxtLogFolderClear(DTNow, saveDay, ConfigurationManager.AppSettings["CommonLog"].ToString());
        }
    }
}
